﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using ELearningSystem.Core.Interface;
using ELearningSystem.Core.Manager.ExtensionMethod;
using ELearningSystem.DAO.DAO;
using ELearningSystem.Gateway.Entities;
using ELearningSystem.Gateway.Gateway;

namespace ELearningSystem.Core.Manager
{
    public class ContentManager : IContentManager
    {
        ContentGateway contentGateway = new ContentGateway();
        public bool SaveOrUpdate(TreeContentDAO treeContent)
        {
            return contentGateway.SaveOrUpdate(treeContent.ToTreeConentEntity());
        }

        public int GetLastPosition(TreeContentDAO parentNode)
        {
            IList<TreeContent> treeContents = contentGateway.GetAllTreeContentsUnderNode(parentNode.ToTreeConentEntity());
            return (treeContents.Count + 1); //Increment The last content position
        }

        public TreeContentDAO GetCategoryNode()
        {
            return contentGateway.GetRoot().ToTreeContentDAO();
        }

        public List<TreeContentDAO> GetAllTopics()
        {
            var treeContents = contentGateway.GetAllTreeContentsUnderNode(contentGateway.GetRoot());
            var contents = new List<TreeContentDAO>();
            foreach (TreeContent treeContent in treeContents)
            {
                TreeContentDAO treeContentDAO = new TreeContentDAO();
                treeContentDAO = treeContent.ToTreeContentDAO();
                contents.Add(treeContentDAO);
            }
            return contents;
        }

        public TreeContentDAO GetTreeContent(int id)
        {
            return contentGateway.GetTreeContent(id).ToTreeContentDAO();
        }
        public string GetTreeDataSource()
        {
            return contentGateway.GetTreeDataSource();
        }

        public ContentDAO GetContent(int treeContentId)
        {
            return contentGateway.GetContent(treeContentId).ToContentDAO();
        }

        public bool IsMultipleAnswer(PracticeDAO practice, out string message)
        {
            bool output = (practice.Answers.FindAll(x => x.IsCorrect).Count > 1) ? true : false;

            message = output ? "Select as many answers you think correct" : "Select any one of the given answers";

            return output;
        }

        public string GetNextTreeNodePath(string treeNodeValue, string xmlData)
        {
            string treexml = HttpContext.Current.Server.MapPath("tree.xml");
            XDocument xdoc = XDocument.Load(treexml);

            var selectedElement = xdoc.Elements("CATEGORY")
                .Elements("TOPIC")
                .Elements("CONTENT")
                .Single(x => x.Attribute("ID").Value == treeNodeValue);
            string path = selectedElement.GetAbsoluteXPath();
            XElement parentElementOfSelectedNode = selectedElement.Parent;
            if (selectedElement.NextNode != null)
            {
                XElement nextElement = (XElement)selectedElement.NextNode;
                path = nextElement.GetAbsoluteXPath();
            }
            else
            {
                var nextParents = parentElementOfSelectedNode.ElementsAfterSelf();
                if (nextParents.Any())
                {
                    XElement nextParent = nextParents.First();
                    if(nextParent.HasElements)
                    {
                        XElement nextElement = nextParent.Element("CONTENT");
                        path = nextElement.GetAbsoluteXPath();
                    }
                    
                }
            }
            return path;
        }

        public string GetPreviousTreeNodePath(string treeNodeValue, string xmlData)
        {
            string treexml = HttpContext.Current.Server.MapPath("tree.xml");
            XDocument xdoc = XDocument.Load(treexml);

            var selectedElement = xdoc.Elements("CATEGORY")
                .Elements("TOPIC")
                .Elements("CONTENT")
                .Single(x => x.Attribute("ID").Value == treeNodeValue);
            string path = selectedElement.GetAbsoluteXPath();
            XElement parentElementOfSelectedNode = selectedElement.Parent;
            if (selectedElement.PreviousNode != null)
            {
                XElement previousElement = (XElement)selectedElement.PreviousNode;
                path = previousElement.GetAbsoluteXPath();
            }
            else
            {
                var previousParents = parentElementOfSelectedNode.ElementsBeforeSelf();
                if (previousParents.Any())
                {
                    XElement previousParent = previousParents.Last();
                    if (previousParent.HasElements)
                    {
                        XElement previousElement = previousParent.Elements("CONTENT").Last();
                        path = previousElement.GetAbsoluteXPath();
                    }
                }

            }
            return path;
        }
    }
}
